import { sortBy, orderBy } from 'lodash-es'

export function getTagGroup(list){
  const listLevelAsc = orderBy(list, ['level', 'showOrder'], ['asc', 'asc'])
  const obj = {};
  const newList = []
  for(let item of listLevelAsc) {
    if(item.parentId) {
      if(!obj[item.parentId]){
        obj[item.parentId] = []
      }
      obj[item.parentId].push(item)
    }else {
      newList.push(item)
    }
  }
  function setSubGroups(arr, parents = []){
    for(let i = 0 ; i < arr.length; i++) {
      const { id } = arr[i];
      arr[i].subGroups = obj[id] || []
      arr[i].parents = [...parents]
      const subParents = parents.concat([{ ...arr[i], subGroups: []}])
      setSubGroups(arr[i].subGroups,  subParents)
    }
  }
  setSubGroups(newList)
  return newList
  // console.log('listLevelAsc', listLevelAsc, newList)
}